//
//  DishManager.m
//  takeout
//
//  Created by Derekc on 12-3-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DishManager.h"
#import "ResolveXML.h"
#import "Constants.h"
#import "DishCategory.h"
#import "Dish.h"

@implementation DishManager

-(NSArray *)getAllDishesBySupId:(NSString *)supplierId{
    
    
    __strong NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_ALL_DISHES_BY_SUPPLIER,supplierId]];
//    CUSTOM_LOG(URIString);
    
    
    
    __strong NSURL *url = [[NSURL alloc] initWithString:[URIString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    __strong NSMutableURLRequest* request = [NSMutableURLRequest new];    
    [request setURL:url];    
    [request setHTTPMethod:@"GET"]; 
    
    __strong NSHTTPURLResponse* response;    
    
    
    
    __strong NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
    
    __strong NSString* responseXMLResult=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    __strong NSError *error;
    
    __strong GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString: responseXMLResult options:0 error:&error];
    
    
    
    
    
    __strong NSArray *dishCategories = [doc.rootElement children]; 
    
    __strong NSMutableArray *result = [[NSMutableArray alloc]init];
    for (GDataXMLElement *element in dishCategories) {
        if ([element isKindOfClass:[GDataXMLElement class]]) {
            
            __strong DishCategory *dishCategory = [[DishCategory alloc] init];
            dishCategory.dishCategoryId = [[[element childAtIndex:0] stringValue] integerValue];
            dishCategory.dishCategoryName = [[element childAtIndex:1] stringValue];
            __strong GDataXMLElement *dishElements = (GDataXMLElement *)[element childAtIndex:2];
            
            __strong NSArray *dishArray = [dishElements children];
            if ([dishArray count] > 0) 
            {
                
                __strong NSMutableArray *dishes =[[NSMutableArray alloc]init];
                for (GDataXMLElement *dishElement in dishArray) {
                    if ([dishElement isKindOfClass:[GDataXMLElement class]]) {
                        
                        __strong Dish *dish = [[Dish alloc] init];
                        dish.dishId = [[[dishElement childAtIndex:0] stringValue] integerValue];
                        dish.dishName = [[dishElement childAtIndex:1] stringValue];
                        dish.imgUrl = [[dishElement childAtIndex:2] stringValue];
                        dish.desc = [[dishElement childAtIndex:3] stringValue];
                        dish.price = [[[dishElement childAtIndex:4] stringValue] floatValue];
                        dish.secondPrice = [[[dishElement childAtIndex:5] stringValue] floatValue];
                        dish.thirdPrice = [[[dishElement childAtIndex:6] stringValue] floatValue];
                        dish.dishCategoryId =  [[[dishElement childAtIndex:7] stringValue] integerValue];
                        
//                        if (dishes == nil) {
//                            dishes = [NSMutableArray arrayWithObject:dish];
//                        }else{
                            [dishes addObject:dish];
//                        }

                    }
                }
                dishCategory.dishes = dishes;
//                if (result == nil) {
//                    result = [NSMutableArray arrayWithObject:dishCategory];
//                }else{
                    [result addObject:dishCategory];
//                }
                
            }
        }
    }
    
    return result;
}

@end
